##########
#Playing to the Gallery: Emotive Rhetoric in Parliaments
#Moritz Osnabruegge, Sara B. Hobolt, Toni Rodon
#This script produces Figure 4
#########


rm(list=ls())

library("ggplot2")
library("data.table")
library("plyr")

#Specify the path here
setwd("")

data = fread("uk_data.csv", encoding="UTF-8")

data$top_topic = as.character(data$top_topic)
data$top_topic[data$top_topic=="fabric of society"] = "Fabric of society"
data$top_topic[data$top_topic=="social groups"] = "Social groups"
data$top_topic[data$top_topic=="welfare and quality of life"] = "Welfare and quality of life"
data$top_topic[data$top_topic=="external relations"] = "External relations"
data$top_topic[data$top_topic=="freedom and democracy"] = "Freedom and democracy"
data$top_topic[data$top_topic=="political system"] = "Political system"
data$top_topic[data$top_topic=="economy"] = "Economy"
data$top_topic[data$top_topic=="no topic"] = "No topic"

data = subset(data, top_topic!="No topic")

ac = function(x){
  m = mean(x$emotive_rhetoric)
  out = m
}

result = ddply(data, c("top_topic"), ac)
colnames(result) = c("top_topic", "emotive_rhetoric")

result$top_topic = factor(result$top_topic, levels = c("Economy", "Political system", "Freedom and democracy", "External relations", "Welfare and quality of life", "Social groups", "Fabric of society"))         


#Figure 4: Average level of emotive rhetoric by topic
p = ggplot(data=result, aes(x=top_topic, y=emotive_rhetoric)) +
    geom_bar(stat="identity") + coord_flip() + 
    ylab("Average Level of Emotive Rhetoric") + 
    scale_y_continuous(breaks=seq(from=-2, to=4, by=1)) +  
    xlab("") + theme_bw() +  
    theme(axis.text.x=element_text(colour="black",size=11),
    axis.text.y=element_text(colour="black", size=11), 
    axis.line=element_line(color="black")) 

pdf("figure_4.pdf",width=6,height=4)
print(p)
dev.off()